package com.shangxia.monomer.study.math.algorithm.strings;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import lombok.var;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.function.Function;

/**
 * @author zhouya38158
 * @Package com.shangxia.monomer.study.math.algorithm.strings
 * @date 2021/7/22 10:36
 */
@Slf4j
public class StringDemo {

    public static void main(String[] args) {
        try {
            System.out.println(isAnagram(null, "adcb"));
        } catch (Exception e) {
            log.error("==", e);
        }
    }

    /**
     * 回文检查
     */
    public static boolean isPalindrome(String s) {
        StringBuilder sb = new StringBuilder();
        for (char c : s.toCharArray()) {
            //如果字符是字母，则为真； 否则为假。
            if (Character.isLetter(c)) {
                sb.append(c);
            }
        }

        String forward = sb.toString().toLowerCase();
        String backward = sb.reverse().toString().toLowerCase();
        return forward.equals(backward);
    }

    /**
     * 反转字符串
     */
    public static String reverseString(String s) {
        return new StringBuilder(s).reverse().toString();
    }

    /**
     * String to date
     */
    public static Date stringToDate(@NonNull String date, @NonNull String format) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.parse(date);
    }

    /**
     * 是否具有相同的字符，与顺序无关
     */
    public static boolean isAnagram(@NonNull String s1, @NonNull String s2) {
        int l1 = s1.length();
        int l2 = s2.length();
        int[] arr1 = new int[256];
        int[] arr2 = new int[256];
        if (l1 != l2) {
            return false;
        }

        for (int i = 0; i < l1; i++) {
            arr1[s1.charAt(i)]++;
            arr2[s2.charAt(i)]++;
        }

        return Arrays.equals(arr1, arr2);
    }

    /**
     *
     */
    public static int findLevenshteinDistance(@NonNull String word1, @NonNull String word2) {
        // If word2 is empty, removing
        int[][] ans = new int[word1.length() + 1][word2.length() + 1];
        for (int i = 0; i <= word1.length(); i++) {
            ans[i][0] = i;
        }

        // if word1 is empty, adding
        for (int i = 0; i <= word2.length(); i++) {
            ans[0][i] = i;
        }

        // None is empty
        for (int i = 1; i <= word1.length(); i++) {
            for (int j = 1; j <= word2.length(); j++) {
                int min = Math.min(Math.min(ans[i][j - 1], ans[i - 1][j]), ans[i - 1][j - 1]);
                ans[i][j] = word1.charAt(i - 1) == word2.charAt(j - 1) ? ans[i - 1][j - 1] : min + 1;
            }
        }
        return ans[word1.length()][word2.length()];
    }

    /**
     * 未知
     */
    public static int compareVersion(String v1, String v2) {
        Function<String, String[]> getVersionComponents = version -> version.replaceAll(".*?((?<!\\w)\\d+([.-]\\d+)*).*", "$1").split("\\.");
        String[] components1 = getVersionComponents.apply(v1);
        String[] components2 = getVersionComponents.apply(v2);
        int length = Math.max(components1.length, components2.length);

        for (int i = 0; i < length; i++) {
            Integer c1 = i < components1.length ? Integer.parseInt(components1[i]) : 0;
            Integer c2 = i < components2.length ? Integer.parseInt(components2[i]) : 0;
            int result = c1.compareTo(c2);
            if (result != 0) {
                return result;
            }
        }
        return 0;
    }
}
